/************************************************************************
 *				SRLOS Team CopyRight
 *@file		mem_slab.h
 *@author	bloceanc
 *@date		7/20/2010
 *@note		kernel small memory block mangerment
 ************************************************************************/
#ifndef	_MEM_SLAB_H_
#define	_MEM_SLAB_H_

/*
 * Comments:
 * 		mem_slab_pool contains many slabs.one slabs contains many slab_chunk, every slab_chunk means an
 * object after sub the slab pointer! 
 */

// common values
#define	MEM_SLAB_MAX_OBJ_SIZE			512
#define	MEM_SLAB_ALLOCATE_PAGE_COUNT	2	// page count when create new slab very time
#define	MEM_SLAB_HEADER_SIZE 			(sizeof(struct mem_slab_struct))
#define	MEM_SLAB_CHUNK_HEADER_SIZE		(sizeof(struct mem_slab_chunk_struct))
#define	MEM_SLAB_MAX_PAGE_COUNT			2	// page count as shunk pointer of slab list

// lock mode
#define	MEM_SLAB_LOCK_LOCKED	1	/* slab locked */
#define	MEM_SLAB_LOCK_UNLOCK	0	/* slab unlocked */

// slab structure
typedef struct mem_slab_struct{
	unsigned int page_count;	/* count of virtual page in this slab */
	unsigned int obj_size;	/* size of object in this slab */
	struct mem_slab_struct *next;	/* next slab structure in the same slab list using same object size */
	struct mem_slab_struct *prev;	/* previous slab structure in the same slab list using same object size */
	unsigned int free_count;	/* count of unused object in this slabe */
	void *freelist;	/* unused object list */
	void *start_object;	/* after initialize slab, we allocate block from start_object first;if there no more,
							allocate from freelist then */
	void *end_object;	/* indicate the bottom address of this slab */
}mem_slab;

// slab list structure
typedef struct mem_slab_pool_struct{
	unsigned int page_count;	/* count of all virtual pages in this slab list */
	unsigned int free_count;	/* count of all unused objects in this slab list */
	unsigned int max_pagecount;	/* the max count of page in this slab list, if great this value, we will
							free virtual page and physical page unused when free object block memory */
	mem_slab *first_slab;
	k_mutex lock;	/* slab lock, we just lock current slab when operating on it */
}mem_slab_pool;

// slab chunk
typedef	struct mem_slab_chunk_struct{
	union _slaborchunk_union{
		mem_slab *slab;	/* which slab belonging to */
		struct mem_slab_chunk_struct *nextfree;	/* the next free chunk in free list */
	}slaborchunk;	 
}mem_slab_chunk;

#endif	/* _MEM_SLAB_H_ */
